/**
 * Created with IntelliJ IDEA.
 *
 * @author: cxs
 * @date: 2023/7/4
 * @time: 9:26
 * @className：CheckServiceImpl
 * @description： ICheckService实现类
 */
package com.bailan.bus.service.impl;

import com.bailan.bus.domain.Car;
import com.bailan.bus.domain.Check;
import com.bailan.bus.domain.Customer;
import com.bailan.bus.domain.Rent;
import com.bailan.bus.mapper.CarMapper;
import com.bailan.bus.mapper.CheckMapper;
import com.bailan.bus.mapper.CustomerMapper;
import com.bailan.bus.mapper.RentMapper;
import com.bailan.bus.service.ICheckService;
import com.bailan.bus.vo.CarVo;
import com.bailan.bus.vo.CheckVo;
import com.bailan.bus.vo.RentVo;
import com.bailan.common.util.DataGridView;
import com.bailan.common.util.RandomUtils;
import com.bailan.common.util.SysConstant;
import com.bailan.common.util.WebUtils;
import com.bailan.sys.domain.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckServiceImpl implements ICheckService {
    @Autowired
     private CheckMapper checkMapper;
    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CustomerMapper customerMapper;
    /**
     * 加载检查单表单信息
     *
     * @param rentid
     * @return
     */
    @Override
    public Map<String, Object> initCheckFormData(String rentid) {
        //查询出租单
        Rent rent=rentMapper.selectByPrimaryKey(rentid);
        //查询客户信息
        Customer customer=customerMapper.queryCustomerByIdentity(rent.getIdentity());
        //查询车辆信息
        Car  car=carMapper.selectByPrimaryKey(rent.getCarnumber());

        Check check=new Check();
        check.setCheckid(RandomUtils.createRandomStringUseTime(SysConstant.CAR_ORDER_JC));
        check.setCheckdate(new Date());
        check.setRentid(rentid);
        User user= (User) WebUtils.getHttpSession().getAttribute("user");
        check.setOpername(user.getRealname());
        Map<String,Object> map=new HashMap<>();
        map.put("rent",rent);
        map.put("customer",customer);
        map.put("car",car);
        map.put("check",check);
        return map;
    }

    /**
     * 添加检查单信息
     *
     * @param checkVo
     */
    @Override
    public void addCheck(CheckVo checkVo) {
        this.checkMapper.addCheck(checkVo);

        Rent rent= this.rentMapper.selectByPrimaryKey(checkVo.getRentid());
        rent.setRentflag(SysConstant.RENT_BACK_TRUE);
        this.rentMapper.updateRentByRentId(rent);

        Car car= this.carMapper.selectByPrimaryKey(rent.getCarnumber());
        car.setIsrenting(SysConstant.RENT_CAR_FALSE);
        this.carMapper.updateCarByCarName(car);
    }

    /**
     * 分页查询
     *
     * @param checkVo
     * @return
     */
    @Override
    public DataGridView queryAllCheck(CheckVo checkVo) {
        Page<Object> page=PageHelper.startPage(checkVo.getPage(), checkVo.getLimit());
        List<Check> data=this.checkMapper.queryAllCheck(checkVo);
        return new DataGridView(page.getTotal(),data);
    }

    /**
     * 修改
     *
     * @param checkVo
     */
    @Override
    public void updateCheck(CheckVo checkVo) {
        this.checkMapper.updateCheckByPrimaryKey(checkVo);
    }

    /**
     * 删除
     *
     * @param checkVo
     */
    @Override
    public void deleteCheck(CheckVo checkVo) {
        this.checkMapper.deleteCheckByCheckId(checkVo.getCheckid());
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteBatchCheck(String[] ids) {
        for (String id: ids) {
            this.checkMapper.deleteCheckByCheckId(id);
        }
    }
}
